//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Interpolation.h"
using namespace Cephei::QL::Math;
#include <gen/QL/Math/Interpolations/Extrapolator.h>
using namespace Cephei::QL::Math::Interpolations;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Math::CInterpolation::CInterpolation () : CExtrapolator(CInterpolation::typeid)
{
    try
    {
#ifdef HANDLE
        _phInterpolation = NULL;
#endif
        _ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (new QuantLib::Interpolation ( ));
        SetExtrapolator (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::CInterpolation::CInterpolation (boost::shared_ptr<QuantLib::Interpolation>& childNative, Object^ owner) : CExtrapolator(CInterpolation::typeid)
{
#ifdef HANDLE
	_phInterpolation = NULL;
#endif
	_ppInterpolation = &childNative;
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation));
}
Cephei::QL::Math::CInterpolation::CInterpolation (QuantLib::Interpolation& childNative, Object^ owner) : CExtrapolator(CInterpolation::typeid)
{
#ifdef HANDLE
	_phInterpolation = NULL;
#endif
	_ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (&childNative);
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation));
    _InterpolationOwner = owner;
    _ExtrapolatorOwner = owner;
}

Cephei::QL::Math::CInterpolation::CInterpolation (CInterpolation^ copy) : CExtrapolator(CInterpolation::typeid)
{
#ifdef HANDLE
	_phInterpolation = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (copy->GetShared());
        _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation));
    }
}
Cephei::QL::Math::CInterpolation::CInterpolation (System::Type^ t) : CExtrapolator(CInterpolation::typeid)
{
#ifdef HANDLE
	_phInterpolation = NULL;
#endif
	if (!t->IsSubclassOf(CInterpolation::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CInterpolation::CInterpolation (QuantLib::Handle<QuantLib::Interpolation>& childNative, Object^ owner)  : CExtrapolator(CInterpolation::typeid)
{
	_phInterpolation = &childNative;
	_ppInterpolation = &static_cast<boost::shared_ptr<QuantLib::Interpolation>>(childNative.currentLink());
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation));
    _InterpolationOwner = owner;
}
Cephei::QL::Math::CInterpolation::CInterpolation (QuantLib::Handle<QuantLib::Interpolation> childNative)  : CExtrapolator(CInterpolation::typeid)
{
	_phInterpolation = &childNative;
	_ppInterpolation = &static_cast<boost::shared_ptr<QuantLib::Interpolation>>(childNative.currentLink());
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CInterpolation::CInterpolation (QuantLib::Interpolation childNative)  : CExtrapolator(CInterpolation::typeid)
{
#ifdef HANDLE
	_phInterpolation = NULL;
#endif
	_ppInterpolation = new boost::shared_ptr<QuantLib::Interpolation> (new QuantLib::Interpolation (childNative));
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation));
}
#endif

Cephei::QL::Math::CInterpolation::~CInterpolation ()
{
    if (_ppInterpolation != NULL)
    {
	    delete _ppInterpolation;
        _ppInterpolation = NULL;
    }
}
Cephei::QL::Math::CInterpolation::!CInterpolation ()
{
    if (_ppInterpolation != NULL)
    {
	    delete _ppInterpolation;
    }
}
QuantLib::Interpolation& Cephei::QL::Math::CInterpolation::GetReference ()
{
    if (_ppInterpolation == NULL) throw gcnew NativeNullException ();
	return **_ppInterpolation;
}
boost::shared_ptr<QuantLib::Interpolation>& Cephei::QL::Math::CInterpolation::GetShared ()
{
    if (_ppInterpolation == NULL) throw gcnew NativeNullException ();
	return *_ppInterpolation;
}
QuantLib::Interpolation* Cephei::QL::Math::CInterpolation::GetPointer ()
{
    if (_ppInterpolation == NULL) throw gcnew NativeNullException ();
	return &**_ppInterpolation;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Interpolation>& Cephei::QL::Math::CInterpolation::GetHandle ()
{
	if (_phInterpolation == NULL)
	{
		_phInterpolation = new Handle<QuantLib::Interpolation> (*_ppInterpolation);
	}
	return *_phInterpolation;
}
#endif
bool Cephei::QL::Math::CInterpolation::HasNative () 
{
	return (_ppInterpolation != NULL);
}

Double Cephei::QL::Math::CInterpolation::Derivative (Double x, Microsoft::FSharp::Core::FSharpOption<Boolean>^ allowExtrapolation)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        bool _allowExtrapolation = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (allowExtrapolation) ? (bool)ValueHelper::Convert (allowExtrapolation->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation)->derivative ( _x,  _allowExtrapolation );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::CInterpolation::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppInterpolation)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::CInterpolation::IsInRange (Double x)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	bool _rv = (bool)(*_ppInterpolation)->isInRange ( _x );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CInterpolation::Primitive (Double x, Microsoft::FSharp::Core::FSharpOption<Boolean>^ allowExtrapolation)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        bool _allowExtrapolation = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (allowExtrapolation) ? (bool)ValueHelper::Convert (allowExtrapolation->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation)->primitive ( _x,  _allowExtrapolation );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CInterpolation::SecondDerivative (Double x, Microsoft::FSharp::Core::FSharpOption<Boolean>^ allowExtrapolation)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        bool _allowExtrapolation = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (allowExtrapolation) ? (bool)ValueHelper::Convert (allowExtrapolation->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation)->secondDerivative ( _x,  _allowExtrapolation );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IInterpolation^ Cephei::QL::Math::CInterpolation::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppInterpolation)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CInterpolation::XMax::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation)->xMax ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CInterpolation::XMin::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation)->xMin ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::IInterpolation^ Cephei::QL::Math::CInterpolation_Factory::Create ()
{
    return gcnew CInterpolation ();
}
